package com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.zhinenghuacangku_zhijian.R;
import com.xiaoxing.zhinenghuacangku_zhijian.R2;
import com.xiaoxing.zhinenghuacangku_zhijian.di.component.DaggerJianCeYuanGongNengXuanZeComponent;
import com.xiaoxing.zhinenghuacangku_zhijian.di.module.JianCeYuanGongNengXuanZeModule;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.contract.JianCeYuanGongNengXuanZeContract;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.presenter.JianCeYuanGongNengXuanZePresenter;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.entity.JianCeYuanGongNengXuanZe;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_ZHI_JIAN_ACTIVITY_JIAN_CE_YUAN_GONG_NENG_XUAN_ZE)
public class JianCeYuanGongNengXuanZeActivity extends BaseActivity<JianCeYuanGongNengXuanZePresenter> implements JianCeYuanGongNengXuanZeContract.View {


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerJianCeYuanGongNengXuanZeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .jianCeYuanGongNengXuanZeModule(new JianCeYuanGongNengXuanZeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_jian_ce_yuan_gong_neng_xuan_ze; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_jian_ce_yuan_gong_neng_xuan_ze));
        //getJianCeYuanGongNengXuanZeData();
    }

    @Override
    public void getJianCeYuanGongNengXuanZeDataSuccess(JianCeYuanGongNengXuanZe entityData) {


    }

    private void getJianCeYuanGongNengXuanZeData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getJianCeYuanGongNengXuanZeData(map);
    }

    @OnClick(R2.id.btn_shou_yang_yuan_shou_yang)
    public void btnShouYangYuanShouYang() {
        Utils.navigation(this, RouterHub.ZHI_NENG_HUA_CANG_KU_ZHI_JIAN_ACTIVITY_JIAN_CE_YUAN_JIAN_CE);
    }

    @OnClick(R2.id.btn_qu_yang_tai_gui_wei)
    public void btnQuYangTaiGuiWei() {
        returnBackSimpleContainer();
    }

    private void returnBackSimpleContainer() {
        showLoading();

        String methodNames = "ReturnBackSimpleContainer";
        SoapParams paramss = null;
        paramss = new SoapParams();
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();
                try {
                    String result = object.getProperty(methodNames + "Result").toString();
                    if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100") && !result.equals("false")) {
                        showMessage("命令发送成功");
                    } else {
                        showMessage("命令发送失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
